/**
 * 业务常量定义
 * 统一管理前端业务相关的常量，避免硬编码
 * 
 * @author Wu.Liang
 * @date 2025-01-30
 */

// ==================== 业务配置常量 ====================

/**
 * 业务配置常量
 */
export const BUSINESS_CONFIG = {
  // 分页配置
  DEFAULT_PAGE_SIZE: 10,
  MAX_PAGE_SIZE: 100,
  
  // 文件上传配置
  MAX_FILE_SIZE: 10 * 1024 * 1024, // 10MB
  ALLOWED_FILE_TYPES: ['jpg', 'jpeg', 'png', 'gif', 'pdf', 'doc', 'docx', 'xls', 'xlsx'],
  
  // 缓存配置
  CACHE_EXPIRE_TIME: 30 * 60 * 1000, // 30分钟
  USER_INFO_CACHE_KEY: 'user_info',
  PERMISSION_CACHE_KEY: 'user_permissions',
  
  // 请求配置
  REQUEST_TIMEOUT: 15000, // 15秒
  RETRY_TIMES: 3,
  
  // 验证配置
  PASSWORD_MIN_LENGTH: 6,
  PASSWORD_MAX_LENGTH: 20,
  USERNAME_MIN_LENGTH: 3,
  USERNAME_MAX_LENGTH: 20,
  PHONE_REGEX: /^1[3-9]\d{9}$/,
  EMAIL_REGEX: /^[^\s@]+@[^\s@]+\.[^\s@]+$/,
  ID_CARD_REGEX: /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/
} as const

// ==================== 业务规则常量 ====================

/**
 * 业务规则常量
 */
export const BUSINESS_RULES = {
  // 用户相关规则
  USER: {
    MAX_LOGIN_ATTEMPTS: 5,
    LOCK_DURATION: 30 * 60 * 1000, // 30分钟
    PASSWORD_EXPIRE_DAYS: 90,
    SESSION_TIMEOUT: 2 * 60 * 60 * 1000 // 2小时
  },
  
  // 物业相关规则
  PROPERTY: {
    MAX_COMPANY_NAME_LENGTH: 50,
    MAX_ADDRESS_LENGTH: 200,
    MAX_DESCRIPTION_LENGTH: 500
  },
  
  // 区域相关规则
  REGION: {
    MAX_NAME_LENGTH: 50,
    MAX_CODE_LENGTH: 20,
    MAX_DESCRIPTION_LENGTH: 200
  },
  
  // 社区相关规则
  COMMUNITY: {
    MAX_NAME_LENGTH: 100,
    MAX_ADDRESS_LENGTH: 200,
    MAX_DESCRIPTION_LENGTH: 500
  },
  
  // 楼栋相关规则
  BUILDING: {
    MAX_NAME_LENGTH: 50,
    MAX_FLOORS: 100,
    MAX_UNITS_PER_FLOOR: 20
  },
  
  // 房户相关规则
  HOUSEHOLD: {
    MAX_NAME_LENGTH: 50,
    MAX_PHONE_LENGTH: 20,
    MAX_ID_CARD_LENGTH: 18
  },
  
  // 停车相关规则
  PARKING: {
    MAX_PLATE_NUMBER_LENGTH: 20,
    MAX_DESCRIPTION_LENGTH: 200
  },
  
  // 投诉相关规则
  COMPLAINT: {
    MAX_TITLE_LENGTH: 100,
    MAX_CONTENT_LENGTH: 1000,
    MAX_REPLY_LENGTH: 500
  },
  
  // 支付相关规则
  PAYMENT: {
    MIN_AMOUNT: 0.01,
    MAX_AMOUNT: 999999.99,
    MAX_DESCRIPTION_LENGTH: 200
  }
} as const

// ==================== 业务状态常量 ====================

/**
 * 业务状态常量
 */
export const BUSINESS_STATUS = {
  // 通用状态
  COMMON: {
    ACTIVE: 'active',
    INACTIVE: 'inactive',
    PENDING: 'pending',
    APPROVED: 'approved',
    REJECTED: 'rejected',
    CANCELLED: 'cancelled',
    COMPLETED: 'completed'
  },
  
  // 用户状态
  USER: {
    NORMAL: 'normal',
    DISABLED: 'disabled',
    LOCKED: 'locked',
    PENDING_VERIFICATION: 'pending_verification',
    VERIFIED: 'verified',
    UNVERIFIED: 'unverified'
  },
  
  // 物业公司状态
  PROPERTY_COMPANY: {
    PENDING_AUDIT: 'pending_audit',
    AUDIT_PASSED: 'audit_passed',
    AUDIT_REJECTED: 'audit_rejected',
    OPERATING: 'operating',
    SUSPENDED: 'suspended',
    CLOSED: 'closed'
  },
  
  // 投诉状态
  COMPLAINT: {
    SUBMITTED: 'submitted',
    PROCESSING: 'processing',
    RESPONDED: 'responded',
    RESOLVED: 'resolved',
    CLOSED: 'closed',
    REOPENED: 'reopened'
  },
  
  // 支付状态
  PAYMENT: {
    PENDING: 'pending',
    PROCESSING: 'processing',
    SUCCESS: 'success',
    FAILED: 'failed',
    CANCELLED: 'cancelled',
    REFUNDED: 'refunded'
  },
  
  // 维修工单状态
  REPAIR_ORDER: {
    SUBMITTED: 'submitted',
    ASSIGNED: 'assigned',
    PROCESSING: 'processing',
    COMPLETED: 'completed',
    CANCELLED: 'cancelled',
    EVALUATED: 'evaluated'
  }
} as const

// ==================== 业务类型常量 ====================

/**
 * 业务类型常量
 */
export const BUSINESS_TYPES = {
  // 用户类型
  USER_TYPE: {
    RESIDENT: 'resident',
    PROPERTY: 'property',
    ADMIN: 'admin',
    SECURITY: 'security',
    MAINTENANCE: 'maintenance',
    VISITOR: 'visitor'
  },
  
  // 区域类型
  REGION_TYPE: {
    PROVINCE: 'province',
    CITY: 'city',
    DISTRICT: 'district',
    STREET: 'street',
    COMMUNITY: 'community'
  },
  
  // 楼栋类型
  BUILDING_TYPE: {
    RESIDENTIAL: 'residential',
    COMMERCIAL: 'commercial',
    MIXED: 'mixed',
    OFFICE: 'office',
    WAREHOUSE: 'warehouse'
  },
  
  // 房户类型
  HOUSEHOLD_TYPE: {
    OWNER: 'owner',
    TENANT: 'tenant',
    FAMILY: 'family',
    COMPANY: 'company'
  },
  
  // 停车位类型
  PARKING_SPACE_TYPE: {
    RESIDENT: 'resident',
    VISITOR: 'visitor',
    COMMERCIAL: 'commercial',
    DISABLED: 'disabled',
    ELECTRIC: 'electric'
  },
  
  // 投诉类型
  COMPLAINT_TYPE: {
    PROPERTY: 'property',
    SECURITY: 'security',
    ENVIRONMENT: 'environment',
    FACILITY: 'facility',
    NOISE: 'noise',
    OTHER: 'other'
  },
  
  // 支付类型
  PAYMENT_TYPE: {
    PROPERTY_FEE: 'property_fee',
    PARKING_FEE: 'parking_fee',
    UTILITY_FEE: 'utility_fee',
    MAINTENANCE_FEE: 'maintenance_fee',
    OTHER_FEE: 'other_fee'
  },
  
  // 支付方式
  PAYMENT_METHOD: {
    WECHAT: 'wechat',
    ALIPAY: 'alipay',
    BANK_CARD: 'bank_card',
    CASH: 'cash',
    TRANSFER: 'transfer'
  }
} as const

// ==================== 业务配置常量 ====================

/**
 * 业务配置常量
 */
export const BUSINESS_SETTINGS = {
  // 系统配置
  SYSTEM: {
    DEFAULT_LANGUAGE: 'zh-CN',
    DEFAULT_TIMEZONE: 'Asia/Shanghai',
    DATE_FORMAT: 'YYYY-MM-DD',
    TIME_FORMAT: 'HH:mm:ss',
    DATETIME_FORMAT: 'YYYY-MM-DD HH:mm:ss'
  },
  
  // 主题配置
  THEME: {
    PRIMARY_COLOR: '#409EFF',
    SUCCESS_COLOR: '#67C23A',
    WARNING_COLOR: '#E6A23C',
    DANGER_COLOR: '#F56C6C',
    INFO_COLOR: '#909399'
  },
  
  // 布局配置
  LAYOUT: {
    SIDEBAR_WIDTH: 200,
    HEADER_HEIGHT: 60,
    FOOTER_HEIGHT: 40,
    CONTENT_PADDING: 20
  },
  
  // 表格配置
  TABLE: {
    DEFAULT_PAGE_SIZE: 10,
    PAGE_SIZE_OPTIONS: [10, 20, 50, 100],
    SHOW_SIZE_CHANGER: true,
    SHOW_QUICK_JUMPER: true,
    SHOW_TOTAL: true
  },
  
  // 表单配置
  FORM: {
    LABEL_WIDTH: 120,
    LABEL_POSITION: 'right',
    SIZE: 'default',
    VALIDATE_ON_RULE_CHANGE: false
  }
} as const

// ==================== 工具函数 ====================

/**
 * 获取业务配置
 * @param category 配置类别
 * @param key 配置键
 * @returns 配置值
 */
export function getBusinessConfig(category: keyof typeof BUSINESS_CONFIG, key: string): any {
  return BUSINESS_CONFIG[category]
}

/**
 * 获取业务规则
 * @param category 规则类别
 * @param key 规则键
 * @returns 规则值
 */
export function getBusinessRule(category: keyof typeof BUSINESS_RULES, key: string): any {
  return BUSINESS_RULES[category][key as keyof typeof BUSINESS_RULES[typeof category]]
}

/**
 * 获取业务状态
 * @param category 状态类别
 * @param key 状态键
 * @returns 状态值
 */
export function getBusinessStatus(category: keyof typeof BUSINESS_STATUS, key: string): string {
  return BUSINESS_STATUS[category][key as keyof typeof BUSINESS_STATUS[typeof category]]
}

/**
 * 获取业务类型
 * @param category 类型类别
 * @param key 类型键
 * @returns 类型值
 */
export function getBusinessType(category: keyof typeof BUSINESS_TYPES, key: string): string {
  return BUSINESS_TYPES[category][key as keyof typeof BUSINESS_TYPES[typeof category]]
}

/**
 * 获取业务设置
 * @param category 设置类别
 * @param key 设置键
 * @returns 设置值
 */
export function getBusinessSetting(category: keyof typeof BUSINESS_SETTINGS, key: string): any {
  return BUSINESS_SETTINGS[category][key as keyof typeof BUSINESS_SETTINGS[typeof category]]
}

/**
 * 验证手机号格式
 * @param phone 手机号
 * @returns 是否有效
 */
export function isValidPhone(phone: string): boolean {
  return BUSINESS_CONFIG.PHONE_REGEX.test(phone)
}

/**
 * 验证邮箱格式
 * @param email 邮箱
 * @returns 是否有效
 */
export function isValidEmail(email: string): boolean {
  return BUSINESS_CONFIG.EMAIL_REGEX.test(email)
}

/**
 * 验证身份证号格式
 * @param idCard 身份证号
 * @returns 是否有效
 */
export function isValidIdCard(idCard: string): boolean {
  return BUSINESS_CONFIG.ID_CARD_REGEX.test(idCard)
}

/**
 * 验证密码强度
 * @param password 密码
 * @returns 是否有效
 */
export function isValidPassword(password: string): boolean {
  return password.length >= BUSINESS_CONFIG.PASSWORD_MIN_LENGTH && 
         password.length <= BUSINESS_CONFIG.PASSWORD_MAX_LENGTH
}

/**
 * 验证用户名格式
 * @param username 用户名
 * @returns 是否有效
 */
export function isValidUsername(username: string): boolean {
  return username.length >= BUSINESS_CONFIG.USERNAME_MIN_LENGTH && 
         username.length <= BUSINESS_CONFIG.USERNAME_MAX_LENGTH
} 